package com.softtown.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.DocTypeEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.DateUtils;
import com.softtown.common.utils.IdWorker;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dao.SysFeedBackDao;
import com.softtown.modules.sys.dto.SysDocAttDTO;
import com.softtown.modules.sys.dto.SysFeedBackDTO;
import com.softtown.modules.sys.entity.SysDocAttEntity;
import com.softtown.modules.sys.entity.SysFeedBackEntity;
import com.softtown.modules.sys.enums.FbHasAttDocEnum;
import com.softtown.modules.sys.enums.FbIsReadEnum;
import com.softtown.modules.sys.enums.FeedbackStatusEnum;
import com.softtown.modules.sys.service.SysDocAttService;
import com.softtown.modules.sys.service.SysFeedBackService;
import com.softtown.modules.sys.vo.SysDocAttVO;
import com.softtown.modules.sys.vo.SysFeedBackVO;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: SysFeedBackServiceImpl
 * @Description: 系统用户反馈Service实现层
 * @Author: fuming
 * @Date: 2019/6/27 15:44
 */
@Service
public class SysFeedBackServiceImpl extends BaseServiceImpl<SysFeedBackDao, SysFeedBackEntity> implements SysFeedBackService {

    @Resource
    private SysDocAttService sysDocAttService;

    /**
     * @param @param [sysFeedBackDTO]
     * @return @return com.github.pagehelper.PageInfo<com.softtown.modules.sys.entity.SysFeedBackEntity>
     * @throws
     * @Title: getFeedbackListByConditions
     * @Description: 获取用户反馈信息和根据条件来查询以及分页Service实现方法
     * @author fuming
     * @date 2019/7/1 21:11
     */
    @Override
    public PageInfo<SysFeedBackEntity> getFeedbackListByConditions(SysFeedBackDTO sysFeedBackDTO) {
        Integer pageSize = sysFeedBackDTO.getPageSize();
        Integer page = sysFeedBackDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<SysFeedBackEntity> feedbackListByConditions =
                baseDao.getFeedbackListByConditions(sysFeedBackDTO.getFbUserId(), sysFeedBackDTO.getPram(),
                        DateUtils.dealDateFormat(sysFeedBackDTO.getBeginDate()),
                        DateUtils.dealDateFormat(sysFeedBackDTO.getEndDate()));
        return new PageInfo<>(feedbackListByConditions);
    }

    /**
     * @param @param [id]
     * @return @return com.softtown.modules.sys.vo.SysFeedBackVO
     * @throws
     * @Title: getFeedbackDetailsById
     * @Description: 根据反馈ID来获取反馈信息以及相对应的公司名称Service实现方法
     * @author fuming
     * @date 2019/7/1 21:20
     */
    @Override
    public SysFeedBackVO getFeedbackDetailsById(Long id) {
        SysFeedBackEntity feedbackDetailsById = baseDao.getFeedbackDetailsById(id);
        // 获取反馈单附件
        List<SysDocAttVO> list = sysDocAttService.selectFbSysDocAttList(id);
        SysFeedBackVO sysFeedBackVO = ConvertUtils.sourceToTarget(feedbackDetailsById, SysFeedBackVO.class);
        sysFeedBackVO.setSysDocAttList(list);
        return sysFeedBackVO;
    }

    /**
     * @param @param [sysFeedBackDTO]
     * @return @return void
     * @throws
     * @Title: addFeedback
     * @Description: 添加反馈Service实现方法
     * @author fuming
     * @date 2019/7/1 21:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFeedback(SysFeedBackDTO sysFeedBackDTO) {
        long fbNum = IdWorker.getId();
        SysFeedBackEntity sysFeedBackEntity = ConvertUtils.sourceToTarget(sysFeedBackDTO, SysFeedBackEntity.class);
        sysFeedBackEntity.setFbNum(String.valueOf(fbNum));
        if (sysFeedBackDTO.getSysDocAttDTOList().size() > 0) {
            sysFeedBackEntity.setFbHasAttDoc(FbHasAttDocEnum.EXIST.value());
        } else {
            sysFeedBackEntity.setFbHasAttDoc(FbHasAttDocEnum.NO.value());
        }
        sysFeedBackEntity.setFbCompId(sysFeedBackDTO.getCompId());
        sysFeedBackEntity.setFbDate(new Date());
        sysFeedBackEntity.setFbIsRead(FbIsReadEnum.UNREAD.value());
        sysFeedBackEntity.setStatus(FeedbackStatusEnum.OPEN.value());
        sysFeedBackEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        insert(sysFeedBackEntity);
        // 添加系统单据附件
        if (sysFeedBackDTO.getSysDocAttDTOList().size() > 0) {
            insertSysDocAttEntity(sysFeedBackDTO.getSysDocAttDTOList(), sysFeedBackEntity.getId());
        }
    }

    /**
     * @param id 反馈id
     * @return void
     * @Description 结束反馈回复时更新状态
     * @Author fuming
     * @Date 2019/12/4 22:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysFbStatusById(Long id) {
        SysFeedBackEntity one =
                baseDao.selectOne(new QueryWrapper<SysFeedBackEntity>().lambda().eq(SysFeedBackEntity::getId, id).eq(SysFeedBackEntity::getIsDeleted, 0));
        if (one != null) {
            one.setStatus(FeedbackStatusEnum.CLOSE.value());
            one.setUpdater(SecurityUser.getUserId());
            one.setUpdateDate(new Date());
            one.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        }
        baseDao.updateById(one);
    }

    /**
     * 添加系统单据附件
     *
     * @param sysDocAttDTOList 系统附件DTO
     * @param fbId             反馈id
     * @return void
     * @author fuming
     * @date 2019/12/3 19:31
     */
    private void insertSysDocAttEntity(List<SysDocAttDTO> sysDocAttDTOList, Long fbId) {
        // 添加系统单据附件
        for (int i = 0; i < sysDocAttDTOList.size(); i++) {
            SysDocAttEntity sysDocAttEntity = new SysDocAttEntity();
            sysDocAttEntity.setDocTypeValue(DocTypeEnum.SYS_FEEDBACK.value());
            sysDocAttEntity.setDocId(fbId);
            sysDocAttEntity.setAttName(sysDocAttDTOList.get(i).getAttName());
            sysDocAttEntity.setAttUrl(sysDocAttDTOList.get(i).getAttUrl());
            sysDocAttEntity.setSctnLineNo(i + 1);
            sysDocAttEntity.setCreator(SecurityUser.getUser().getId());
            sysDocAttEntity.setCreateDate(new Date());
            sysDocAttEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
            sysDocAttService.insert(sysDocAttEntity);
        }
    }
}
